const fs = require('fs');
const path = require('path');

// 定义需要添加name属性的文件及其对应的name值
const filesToModify = [
    { file: 'src/pages/llhz/index.vue', name: 'wx_pages_llhz_index' },
    { file: 'src/pages/home/index.vue', name: 'wx_pages_home_index' },
    { file: 'src/pages/zhfw/index.vue', name: 'wx_pages_zhfw_index' },
    { file: 'src/pages/djzh/index.vue', name: 'wx_pages_djzh_index' },
    { file: 'src/pages/zhfw/courseDetail.vue', name: 'wx_pages_zhfw_courseDetail' },
    { file: 'src/pages/fzbz/index.vue', name: 'wx_pages_fzbz_index' },
    { file: 'src/pages/consult/index.vue', name: 'wx_pages_consult_index' },
    { file: 'src/pages/consult/consultIm.vue', name: 'wx_pages_consult_consultIm' },
    { file: 'src/pages/signIn.vue', name: 'wx_pages_signIn' },
    { file: 'src/pages/zhfw/communityTeach.vue', name: 'wx_pages_zhfw_communityTeach' },
    { file: 'src/pages/djzh/timeRecall.vue', name: 'wx_pages_djzh_timeRecall' },
    { file: 'src/pages/djzh/activityDetail.vue', name: 'wx_pages_djzh_activityDetail' },
    { file: 'src/pages/djzh/activityAudit.vue', name: 'wx_pages_djzh_activityAudit' },
    { file: 'src/pages/djzh/cultureDetail.vue', name: 'wx_pages_djzh_cultureDetail' },
    { file: 'src/pages/djzh/activitySubmit.vue', name: 'wx_pages_djzh_activitySubmit' },
    { file: 'src/pages/djzh/massPage.vue', name: 'wx_pages_djzh_massPage' },
    { file: 'src/pages/djzh/culturePage.vue', name: 'wx_pages_djzh_culturePage' },
    { file: 'src/pages/zhfw/careCenterPage.vue', name: 'wx_pages_zhfw_careCenterPage' },
    { file: 'src/pages/zhfw/youthPage.vue', name: 'wx_pages_zhfw_youthPage' },
    { file: 'src/pages/zhfw/communityPage.vue', name: 'wx_pages_zhfw_communityPage' },
    { file: 'src/pages/zhfw/hospitalsPage.vue', name: 'wx_pages_zhfw_hospitalsPage' },
    { file: 'src/pages/zhfw/courseSubmit.vue', name: 'wx_pages_zhfw_courseSubmit' },
    { file: 'src/pages/zhfw/doctorResource.vue', name: 'wx_pages_zhfw_doctorResource' },
    { file: 'src/pages/zhfw/myMake.vue', name: 'wx_pages_zhfw_myMake' },
    { file: 'src/pages/zhfw/myNotice.vue', name: 'wx_pages_zhfw_myNotice' },
    { file: 'src/pages/zhfw/serviceAged.vue', name: 'wx_pages_zhfw_serviceAged' },
    { file: 'src/pages/zhfw/serviceAgedDetail.vue', name: 'wx_pages_zhfw_serviceAgedDetail' },
    { file: 'src/pages/webview/webview.vue', name: 'wx_pages_webview_webview' },
    { file: 'src/pages/fzbz/qmfzTable.vue', name: 'wx_pages_fzbz_qmfzTable' },
    { file: 'src/pages/zhfw/hospitalNoticeDetail.vue', name: 'wx_pages_zhfw_hospitalNoticeDetail' },
    { file: 'src/pages/fzbz/qmfzDetail.vue', name: 'wx_pages_fzbz_qmfzDetail' },
    { file: 'src/pages/fzbz/businessPro.vue', name: 'wx_pages_fzbz_businessPro' },
    { file: 'src/pages/fzbz/dispute.vue', name: 'wx_pages_fzbz_dispute' },
    { file: 'src/pages/fzbz/disputeDetail.vue', name: 'wx_pages_fzbz_disputeDetail' },
    { file: 'src/pages/fzbz/disputeSubmit.vue', name: 'wx_pages_fzbz_disputeSubmit' },
    { file: 'src/pages/fzbz/disputeComplainSubmit.vue', name: 'wx_pages_fzbz_disputeComplainSubmit' },
    { file: 'src/pages/fzbz/disputeComplain.vue', name: 'wx_pages_fzbz_disputeComplain' },
    { file: 'src/pages/llhz/sharedGrandmother.vue', name: 'wx_pages_llhz_sharedGrandmother' },
    { file: 'src/pages/llhz/sharedDetail.vue', name: 'wx_pages_llhz_sharedDetail' },
    { file: 'src/pages/llhz/governmentPublic.vue', name: 'wx_pages_llhz_governmentPublic' },
    { file: 'src/pages/llhz/fifteenMinutes.vue', name: 'wx_pages_llhz_fifteenMinutes' },
    { file: 'src/pages/llhz/noticeDetail.vue', name: 'wx_pages_llhz_noticeDetail' }
];

// 处理每个文件
filesToModify.forEach(({ file, name }) => {
    try {
        const filePath = path.resolve(file);

        // 检查文件是否存在
        if (!fs.existsSync(filePath)) {
            console.log(`文件不存在: ${filePath}`);
            return;
        }

        // 读取文件内容
        let content = fs.readFileSync(filePath, 'utf8');

        // 检查是否已有name属性
        const hasName = /name\s*:\s*['"][\w_]+['"]/g.test(content);

        if (hasName) {
            // 替换现有的name属性
            content = content.replace(/name\s*:\s*['"][\w_]+['"]/g, `name: '${name}'`);
        } else {
            // 添加新的name属性
            // 查找export default位置
            const exportDefaultMatch = content.match(/export\s+default\s*{/);
            if (exportDefaultMatch) {
                const position = exportDefaultMatch.index + exportDefaultMatch[0].length;
                content = content.slice(0, position) + `\n  name: '${name}',` + content.slice(position);
            } else {
                // 如果没有找到export default，尝试在<script>标签后添加
                const scriptMatch = content.match(/<script>\s*/);
                if (scriptMatch) {
                    const position = scriptMatch.index + scriptMatch[0].length;
                    content = content.slice(0, position) +
                        `\nexport default {\n  name: '${name}',\n};\n` +
                        content.slice(position);
                } else {
                    console.log(`无法在文件中找到适当位置添加name属性: ${filePath}`);
                    return;
                }
            }
        }

        // 写入修改后的内容
        fs.writeFileSync(filePath, content, 'utf8');
        console.log(`已成功添加name属性到文件: ${filePath}`);
    } catch (error) {
        console.error(`处理文件时出错 ${file}:`, error);
    }
});

console.log('所有文件处理完成!'); 